<script lang="ts" setup>
	import { onShow } from '@dcloudio/uni-app'
	import { ref } from 'vue'
	import { deletePatientApi, getPatientApi } from '../../apis/patient'
	import { PatientList } from '../../types/patient'
	import { useConsultStore } from '../../stores/consult'
	//修改标题
	const props = defineProps<{ type: 'consult' | 'archive' }>()

	if (props.type == 'consult') {
		uni.setNavigationBarTitle({
			title: '选择患者',
		})
	}

	//定义患者列表
	const Patients = ref<PatientList[]>([])
	//获取患者数据
	const getPatients = async () => {
		const res = await getPatientApi()
		Patients.value = res
		// console.log(res)
	}
	//选择患者
	const store = useConsultStore()

	const selPatient = (id: string) => {
		store.patientId = id
	}

	const swipeOptions = ref([
		{
			text: '删除',
			style: {
				backgroundColor: '#dd524d',
				color: '',
				fontSize: '',
			},
		},
	])

	//对身份证信息进行脱敏处理
	const scretIdCard = (idCard: string) => {
		return idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2')
	}
	//左滑删除
	const Leftdelete = async (item) => {
		uni.showModal({
			title: '提示',
			content: '你真的确定要删除吗?',
			success: async function (res) {
				if (res.confirm) {
					//调用删除接口  根据下标进行删除
					await deletePatientApi(item.id)
					Patients.value = Patients.value.filter((v) => v.id !== item.id)
					//删除本地的仓库里面的患者Id 避免后续误操作
					store.patientId = ''
				} else {
					console.log('不删除别点~')
				}
			},
		})
	}

	const go = () => {
		uni.navigateTo({ url: '/subpkg_consult/payment/payment' })
	}
	onShow(() => {
		getPatients()
	})
</script>
<template>
	<scroll-page>
		<view class="archive-page">
			<view class="archive-tips">最多可添加6人</view>
			<uni-swipe-action>
				<uni-swipe-action-item
					v-for="item in Patients"
					:key="item.id"
					:right-options="swipeOptions"
					@click="Leftdelete(item)"
				>
					<view
						@tap="selPatient(item.id)"
						:class="{ active: store.patientId === item.id }"
						class="archive-card"
					>
						<view class="archive-info">
							<text class="name">{{ item.name }}</text>
							<text class="id-card">
								{{ scretIdCard(item.idCard) }}
							</text>
							<text v-if="item.defaultFlag === 1" class="default">默认</text>
						</view>
						<view class="archive-info">
							<text class="gender">{{ item.genderValue }}</text>
							<text class="age">{{ item.age }}岁</text>
						</view>
						<navigator
							hover-class="none"
							class="edit-link"
							:url="`/subpkg_archive/form/form?id=${item.id}`"
						>
							<uni-icons
								type="icon-edit"
								size="20"
								color="#16C2A3"
								custom-prefix="iconfont"
							/>
						</navigator>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>

			<!-- 添加按钮 -->
			<view v-if="Patients.length < 6 && type !== 'consult'" class="archive-card">
				<navigator class="add-link" hover-class="none" url="/subpkg_archive/form/form">
					<uni-icons color="#16C2A3" size="24" type="plusempty" />
					<text class="label">添加患者</text>
				</navigator>
			</view>
			<!-- 出现下一步的条件: type=consult && 患者的Id不为空 -->
			<view v-if="type === 'consult' && store.patientId !== ''">
				<button @click="go" class="uni-button">下一步</button>
			</view>
		</view>
	</scroll-page>
</template>
<style lang="scss">
	@import './index.scss';
</style>
